﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class newwordAction : CWebBase
{
    public CDatabase _db = new CDatabase();

    protected void Page_Load(object sender, EventArgs e)
    {
        String action = CheckNull(Request["action"]);

        _db.GetConnection();

        // Add New Word
        if (action == "saveNewWord") {
            Hashtable result = new Hashtable();
            string succ = "Y";
            try
            {
                if (Userinfo.UserID != "")
                {
                    string Word = CheckNull(Request["Word"]);
                    string WordClass = CheckNull(Request["WordClass"]);
                    string WordMean = CheckNull(Request["WordMean"]);
                    string HeadWord = CheckNull(Request["HeadWord"]);
                    string WordLevel = CheckNull(Request["WordLevel"]);
                    string LevelId = CheckNull(Request["LevelId"]);
                    string UnitNum = CheckNull(Request["UnitNum"]);

                    string sql = "Insert into ANALYZE_NEW_WORD (Word, SeriseName, LevelId, WordClass, WordMean, HeadWord, WordLevel, UnitNum, ModDate, ModUser) values" +
                                 "('" + Word + "', (Select LevelName From ANALYZE_WORD_LEVEL Where LevelId = '" + LevelId + "'), '" + LevelId + "', '" + WordClass + "', '" +
                                     WordMean + "', '" + HeadWord + "', '" + WordLevel + "', '" +
                                     UnitNum + "', GETDATE(), '" + Userinfo.UserID + "')";

                    result.Add("sql", sql);
                    SqlDataReader dr = _db.GetQueryResult(sql);
                    dr.Close();
                }
                else {
                    succ = "auth";
                }
            }
            catch (Exception ex)
            {
                succ = "N";
            }
            finally
            {
                result.Add("succ", succ);
                Response.AddHeader("'Content-type", "application/json");
                Response.Write(getJson(result));
            }
            return;
        }


        // Delete New Word
        if (action == "delWord")
        {
            Hashtable result = new Hashtable();
            string succ = "Y";
            try
            {
                if (Userinfo.UserID != "")
                {
                    String Word = CheckNull(Request["Word"]);
                    String WordClass = CheckNull(Request["WordClass"]);
                    String LevelId = CheckNull(Request["LevelId"]);
                    String UnitNum = CheckNull(Request["UnitNum"]);

                    if (isEmpty(Word) || isEmpty(WordClass) || isEmpty(LevelId) || isEmpty(UnitNum))
                    {
                        succ = "valid";
                    }
                    else
                    {

                        String sql = "Delete from ANALYZE_NEW_WORD where Word='" + Word + "' and WordClass='" + WordClass + "' and LevelId='" + LevelId + "' and UnitNum='" + UnitNum + "'";
                        SqlDataReader dr = _db.GetQueryResult(sql);
                        dr.Close();
                    }
                }
                else
                {
                    succ = "auth";
                }
            }
            catch (Exception ex){
                succ = "N";
            }finally{
                result.Add("succ", succ);
                Response.AddHeader("'Content-type", "application/json");
                Response.Write(getJson(result));
            }
            return;
        }
    }

    protected void Page_Unload(object sender, EventArgs e)
    {
        _db.ReleaseConnection();
    }
}
